﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
using HU8.Helpers.Files;
using HU8.Helpers.Base;
using System.Text.RegularExpressions;
using System.Text;

namespace HU8.Web.FileExplorer.FileExplorer.WebServices
{
    /// <summary>
    /// TextFileService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    [System.Web.Script.Services.ScriptService]
    public class TextFileService : System.Web.Services.WebService
    {
        private FileExplorerConfig config;
        public TextFileService()
        {
            config = new FileExplorerConfig(Server);
        }

        /// <summary>
        /// 获取文本内容
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public string GetFileContent(string path)
        {
            path = path.Replace("~/", config.BasePath);
            string physicalPath = Server.MapPath(path);
            string text = FileHelper.ReadTextFile(physicalPath);
            return text;
        }

        /// <summary>
        /// 保存文本内容
        /// </summary>
        /// <param name="path"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public JsonResponse SaveFile(string path, string content)
        {
            string decoded = Microsoft.JScript.GlobalObject.unescape(content);
            path = Server.MapPath(path);
            return FileHelper.SaveTextFile(path, decoded);
        }
    }
}
